/**
 * 商家订单操作
 *
 * @project sky-take-out2
 * @author MECHREVO
 * @date 2023/12/12 16:29:43
 * @version 1.0
 */
package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderAdminService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@RequiredArgsConstructor
public class OrderController {
    private final OrderAdminService orderAdminService;

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    public Result<?> selectConditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询条件为：{}", ordersPageQueryDTO);
        PageResult pageResult = orderAdminService.selectConditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("details/{id}")
    public Result selectOrderDetailByOrderId(@PathVariable Long id) {
        log.info("查询订单详情，订单id为：{}", id);
        OrderVO orderVO = orderAdminService.selectOrderDetailByOrderId(id);
        return Result.success(orderVO);
    }

    /**
     * 订单统计
     *
     * @return
     */
    @GetMapping("/statistics")
    public Result selectStatistics() {
        List<OrderStatisticsVO> orderStatisticsVO = orderAdminService.selectStatistics();
        for (OrderStatisticsVO statisticsVO : orderStatisticsVO) {
            return Result.success(statisticsVO);
        }
        return Result.success();
    }

    /**
     * 确认订单
     *
     * @param ordersConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    public Result<?> confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("确认订单，订单id为：{}", ordersConfirmDTO);
        orderAdminService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒绝订单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    public Result<?> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒绝订单，订单id为：{}", ordersRejectionDTO);
        orderAdminService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     *
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    public Result<?> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单，订单id为：{}", ordersCancelDTO);
        orderAdminService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result<?> delivery(@PathVariable Long id) {
        OrdersConfirmDTO ordersConfirmDTO = new OrdersConfirmDTO();
        ordersConfirmDTO.setId(id);
        log.info("派送订单，订单id为：{}", ordersConfirmDTO.getId());
        orderAdminService.delivery(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 订单完成
     *
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    public Result<?> complete(@PathVariable Long id) {
        log.info("订单完成，订单id为：{}", id);
        orderAdminService.complete(id);
        return Result.success();
    }
}
